//
//  HUD.swift
//  pBox
//
//  Created by TianChenIMac1 on 2019/9/23.
//  Copyright © 2019 skymesh. All rights reserved.
//

import UIKit
import SVProgressHUD
import UIKit
public class HUD {
    public enum Position {
        case center
        case bottom
    }
    private static let `default` : HUD = HUD()
    
    // MARK:系统弹框
    @objc static func showSystemAlert(title :String?,message :String?,titleArray :[String],alertType :UIAlertController.Style,completeBlock :@escaping((Int)->Void)){
        
       let alertSheet = UIAlertController(title: title, message: message, preferredStyle: alertType)
       // 2 命令（样式：退出Cancel，警告Destructive-按钮标题为红色，默认Default）
        for item in titleArray.enumerated() {
            
            let action = UIAlertAction.init(title: item.element, style: .default) { (_) in
                completeBlock(item.offset)
            }
            alertSheet.addAction(action)
        }
       // 3 跳转
        
        UIApplication.shared.keyWindow?.rootViewController?.present(alertSheet, animated: true, completion: nil)
        
    }
    
    public static func showMessage(_ message: String?, hidden delay: TimeInterval = 1.5, position: Position = HUD.Position.center, completion: SVProgressHUDDismissCompletion? = nil) {
        
        if case .bottom = position   {
            SVProgressHUD.setOffsetFromCenter(.init(horizontal: 0, vertical: CGFloat(ScreenWidth*0.5-SafeAreaBottom - 50)))
        } else {
            SVProgressHUD.setOffsetFromCenter(.init(horizontal: 0, vertical: 0))
        }

        SVProgressHUD.setBackgroundColor(UIColor.set_Color(82, green: 104, blue: 117, alpha: 1.0))
        SVProgressHUD.setForegroundColor(.white)
    }
    
    @discardableResult
    public static func showTip(_ text: String?, hidden delay: TimeInterval = 1.5, position: Position = HUD.Position.center, completion: SVProgressHUDDismissCompletion? = nil) -> HUD {
        
        HUD.dismiss()
        return HUD.default.showTip(text!).set(position: position).infoImage(nil).backgroudColor(UIColor.set_Color(82, green: 104, blue: 117, alpha: 1.0)).dismiss(delay, completion: completion).textColor(UIColor.white)
    }
    
    @discardableResult
    public static func showSuccess(_ text: String?, hidden delay: TimeInterval = 1.5, completion: SVProgressHUDDismissCompletion? = nil) -> HUD {
        return HUD.default.showSuccess(text).dismiss(delay, completion: completion)
    }
    
    @discardableResult
    public static func showProgress(_ progress: Float = 0.0, _ text: String?) -> HUD {
        return HUD.default.showProgress(progress, text)
    }
    
    @discardableResult
    public static func showError(_ text: String?, hidden delay: TimeInterval = 1.5, completion: SVProgressHUDDismissCompletion? = nil) -> HUD {
        return HUD.default.showError(text).dismiss(delay, completion: completion)
    }
    
    
    public static func showWait(_ text: String?) {
        DispatchQueue.main.async {
            SVProgressHUD.setDefaultMaskType(.black)
        }
        
        let _ = HUD.default.showWait(text).set(position: .center)
    }
    
    public static func dismiss(_ delay: TimeInterval = 0.0, _ completion: SVProgressHUDDismissCompletion? = nil) {
        HUD.default.dismiss(delay, completion: completion)
    }
    
    @discardableResult
    private func showTip(_ text: String?) -> HUD {
        DispatchQueue.main.async {
        
            HUD.showTip(text)
        }
        return self
    }
    
    private func showWait(_ text: String?) -> HUD {
        DispatchQueue.main.async {
           // HUD.showWait(text)
            
            SVProgressHUD.show(withStatus: text)
        }
        return self
    }
    
    
    
    @discardableResult
    private func showSuccess(_ text: String?) -> HUD {
        DispatchQueue.main.async {
            HUD.showSuccess(text)
        }
        return self
    }
    
    @discardableResult
    private func showError(_ error: String?) -> HUD {
        DispatchQueue.main.async {
            HUD.showError(error)
          //  SVProgressHUD.showError(withStatus: error)
        }
        return self
    }
    
    @discardableResult
    private func showProgress(_ progress: Float, _ text: String?) -> HUD {
        DispatchQueue.main.async {
            SVProgressHUD.showProgress(progress, status: text)
        }
        return self
    }
    
    @discardableResult
    private func dismiss(_ delay: TimeInterval = 1.0, completion: SVProgressHUDDismissCompletion?) -> HUD {
        DispatchQueue.main.async {
            SVProgressHUD.dismiss(withDelay: delay, completion: completion)
        }
        
        return self
    }
    
    @discardableResult
    public func set(position: HUD.Position = .center) -> HUD {
        switch position {
        case .bottom:
            DispatchQueue.main.async {
                SVProgressHUD.setOffsetFromCenter(.init(horizontal: 0, vertical: CGFloat(ScreenWidth * 0.5-SafeAreaBottom - 50)))
            }
        default:
            break
        }
        return self
    }
    
    @discardableResult
    public func set(mask: SVProgressHUDMaskType) -> HUD {
        DispatchQueue.main.async {
            SVProgressHUD.setDefaultMaskType(mask)
        }
        return self
    }
    
    @discardableResult
    public func infoImage(_ image: UIImage?) -> HUD {
        guard let img = image, img.size != .zero else {
            DispatchQueue.main.async {
            }
            return self
        }
        
        DispatchQueue.main.async {
   
            SVProgressHUD.setInfoImage(img)
        }
        return self
    }
    
    @discardableResult
    public func backgroudColor(_ color: UIColor) -> HUD {
        DispatchQueue.main.async {
            SVProgressHUD.setBackgroundColor(color)
        }
        return self
    }
    
    @discardableResult
    public func textColor(_ color: UIColor) -> HUD {
        DispatchQueue.main.async {
            SVProgressHUD.setForegroundColor(color)
        }
        return self
    }
    
    @discardableResult
    public func textFont(_ font: UIFont) -> HUD {
        DispatchQueue.main.async {
            SVProgressHUD.setFont(font)
        }
        return self
    }
}
